﻿using Coding4Fun.Toolkit.Controls;
using Microsoft.Phone.Net.NetworkInformation;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using Tin18.Model;
using Tin18.Resources;

namespace Tin18.Library
{
    public class ListComment : INotifyPropertyChanged
    {
        string ID = "";
        public ListComment(string _id)
        {
            Error = false;
            SysError = false;
            Busy = true;

            ID = _id;

            AddMoreItems();

            fetchMoreDataCommand = new DelegateCommand(
                obj =>
                {
                    if (busy)
                    {
                        return;
                    }
                    Busy = true;
                    ThreadPool.QueueUserWorkItem(
                        delegate
                        {
                            Deployment.Current.Dispatcher.BeginInvoke(
                                delegate
                                {
                                        AddMoreItems();
                                });
                        });

                });
        }
        int pageIndex = 0;
        void AddMoreItems()
        {
            int start = pageIndex++;
            int end = 30;
            string Param = MyLibrary.RanDomBase64() + MyLibrary.Base64(string.Format(AppResources.GetComment,start,end,ID));
            string URL = AppResources.Server + Param;
            WebClient webclient = new WebClient();
            webclient.DownloadStringCompleted += webclient_DownloadStringCompleted;
         
            webclient.DownloadStringAsync(new Uri(URL));
        }
        void webclient_DownloadStringCompleted(object sender, System.Net.DownloadStringCompletedEventArgs e)
        {
            try
            {
                string json = e.Result;
           
                if (!string.IsNullOrEmpty(json))
                {
                    RootCommentObject _Object = JsonConvert.DeserializeObject<RootCommentObject>(json);
               if(_Object!=null)
                   for (int i = 0; i < _Object.list.Count; i++)
                   {
                       items.Add(_Object.list[i]);
                   }

                    Busy = false;
                }
            }
            catch
            {
                if (DeviceNetworkInformation.IsNetworkAvailable)
                    SysError = true;
                else
                    Error = true;
                Busy = false;
              
            }
           

        }
        private static ToastPrompt GetToastWithImgAndTitle(string _title, string _content)
        {
            return new ToastPrompt
            {
                Title = _title,
                TextOrientation = System.Windows.Controls.Orientation.Vertical,
                Message = _content
            };
        }
        readonly DelegateCommand fetchMoreDataCommand;

        public ICommand FetchMoreDataCommand
        {
            get
            {
                return fetchMoreDataCommand;
            }
        }

        readonly ObservableCollection<DataComment> items = new ObservableCollection<DataComment>();

        public ObservableCollection<DataComment> ItemsOfList
        {
            get
            {
                return items;
            }
        }

        bool busy;

        public bool Busy
        {
            get
            {
                return busy;
            }
            set
            {
                if (busy == value)
                {
                    return;
                }
                busy = value;
                OnPropertyChanged(new PropertyChangedEventArgs("Busy"));
            }
        }
        bool error;
        public bool Error
        {
            get
            {
                return error;
            }
            set
            {
                if (error == value)
                {
                    return;
                }
                error = value;
                OnPropertyChanged(new PropertyChangedEventArgs("Error"));
            }
        }
        bool syserror;
        public bool SysError
        {
            get
            {
                return syserror;
            }
            set
            {
                if (syserror == value)
                {
                    return;
                }
                syserror = value;
                OnPropertyChanged(new PropertyChangedEventArgs("SysError"));
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            var tempEvent = PropertyChanged;
            if (tempEvent != null)
            {
                tempEvent(this, e);
            }
        }
    }
}
